"""
模型文件，主要是数据库中各个表结构
"""

from exts import db

# 中间表

Exam_Question_table = db.Table('exam_question', db.Column('exam_id', db.Integer, db.ForeignKey('exam.id')),
                               db.Column('question_id', db.Integer,
                                         db.ForeignKey('question.id')))

class stuAns(db.Model):
    __tablename__ = 'stu_ans'
    stu_id = db.Column(db.Integer, db.ForeignKey(
        'student.id'), primary_key=True)
    exam_id = db.Column(db.Integer, db.ForeignKey(
        'exam.id'), primary_key=True)
    question_id = db.Column(db.Integer, db.ForeignKey(
        'question.id'), primary_key=True)
    ans = db.Column(db.String(20))

class Work_assign(db.Model):
    __tablename__ = 'work_assign'
    teacher_id = db.Column(db.Integer, db.ForeignKey(
        'teacher.id'), primary_key=True)
    exam_id = db.Column(db.Integer, db.ForeignKey(
        'exam.id'), primary_key=True)
    # 该类型试卷老师已经判的数量
    work_finished = db.Column(db.Integer)
    # 该类型试卷老师需要判多少份数
    work_count = db.Column(db.Integer)

    wa_exam = db.relationship('Exam', back_populates='teachers')
    teacher = db.relationship('teacherModel', back_populates='exams')


class Student_Exam(db.Model):
    __tablename__ = 'student_exam'
    stu_id = db.Column(db.Integer, db.ForeignKey(
        'student.id'), primary_key=True)
    exam_id = db.Column(db.Integer, db.ForeignKey(
        'exam.id'), primary_key=True)
    # 得分
    score = db.Column(db.Integer)
    # 卷子是否提交,0为没有,1为提交
    finished = db.Column(db.String(1))
    # 是否缴费,0为没有,1为提交
    paid = db.Column(db.String(1))
    exam = db.relationship('Exam', back_populates='students')
    student = db.relationship('studentModel', back_populates='exams')


class UserModel(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    userName = db.Column(db.String(100), nullable=False)
    passWord = db.Column(db.String(100), nullable=False)
    email = db.Column(db.String(100), unique=True)
    trueName = db.Column(db.String(100))
    phone = db.Column(db.String(100))
    ring = db.Column(db.Integer)
    userType = db.Column(db.String(100), nullable=False)

    __mapper_args__ = {
        'polymorphic_identity': 'user',
        'polymorphic_on': userType
    }


class studentModel(UserModel):
    __tablename__ = "student"
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    school = db.Column(db.String(100), nullable=False)
    schoolId = db.Column(db.String(100), nullable=False)
    yard = db.Column(db.String(100))
    major = db.Column(db.String(100))
    inSchoolTime = db.Column(db.String(100))
    __mapper_args__ = {
        'polymorphic_identity': 'student',
    }
    exams = db.relationship('Student_Exam', back_populates='student')


class teacherModel(UserModel):
    __tablename__ = "teacher"
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    tSchool = db.Column(db.String(100), nullable=False)
    teachId = db.Column(db.String(100), nullable=False)
    __mapper_args__ = {
        'polymorphic_identity': 'teacher',
    }
    exams = db.relationship('Work_assign', back_populates='teacher')


class workerModel(UserModel):
    __tablename__ = "worker"
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    workId = db.Column(db.String(100), nullable=False)
    __mapper_args__ = {
        'polymorphic_identity': 'worker',
    }


class Exam(db.Model):
    __tablename__ = 'exam'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    sign_time = db.Column(db.DateTime, nullable=True)
    do_time = db.Column(db.DateTime, nullable=True)
    # 这个只用来建立
    questions = db.relationship(
        'Question', secondary=Exam_Question_table, backref=db.backref('exams'), lazy='dynamic')
    students = db.relationship('Student_Exam', back_populates='exam')
    teachers = db.relationship('Work_assign', back_populates='wa_exam')


class Question(db.Model):
    __tablename__ = 'question'
    id = db.Column(db.Integer, primary_key=True)
    # 题干
    ctx = db.Column(db.UnicodeText, nullable=False)
    # 题目类型 0 单选 1 多选 2 简答题
    question_type = db.Column(db.Integer, nullable=False)
    # 如果是选择题, 记录 A,B, C或D
    # 如果是简答题(因为是英语卷子所以默认只有单词), 记录单词
    correct_ans = db.Column(db.String(20), nullable=True)
